@ECHO OFF

SetLocal EnableDelayedExpansion

SET __original=%cd%
SET __startTime=%DATE% %TIME%
SET __local=%~dp0

IF %1 == "" (
	GOTO Fail
)

IF NOT EXIST %1 GOTO Fail

CD /D %1

IF "%2" == "" GOTO Fail
IF "%3" == "" GOTO Fail

IF /I NOT %2 == Win32:Debug (
	IF /I NOT %2 == Win32:Release (
		IF /I NOT %2 == x64:Debug (
			IF /I NOT %2 == x64:Release (
				GOTO FailInvalidTarget
			)
		)
	)
)

SET "target=%2"

IF /I NOT %3 == msvc10 (
	IF /I NOT %3 == msvc9 (
		GOTO FailInvalidMSVCVersion
	)
)

SET "version=%3"

FOR /F "tokens=1,2 delims=:" %%a IN ( "%target%" ) DO (
	SET "plat=%%a"
	SET "config=%%b"
)

IF NOT EXIST !plat! (
	MKDIR !plat!
)

CD !plat!

IF NOT EXIST %version% (
	MKDIR %version%
)

CD %version%

IF NOT "%NUMBER_OF_PROCESSORS%" == "" (
	SET "processors=%NUMBER_OF_PROCESSORS%"
) ELSE (
	SET "processors=4"
)

IF /I %version% == msvc10 (
	IF /I !plat! == Win32 (
		cmake -G "Visual Studio 10" -DLLVM_TARGETS_TO_BUILD=X86 -DBUILD_SHARED_LIBS=FALSE -DLLVM_INCLUDE_EXAMPLES=FALSE -DLLVM_INCLUDE_TESTS=FALSE -DLLVM_INCLUDE_TOOLS=FALSE ..\..
	) ELSE (
		cmake -G "Visual Studio 10 Win64" -DLLVM_TARGETS_TO_BUILD=X86 -DBUILD_SHARED_LIBS=FALSE -DLLVM_INCLUDE_EXAMPLES=FALSE -DLLVM_INCLUDE_TESTS=FALSE -DLLVM_INCLUDE_TOOLS=FALSE ..\..
	)
	
	CALL "%VS100COMNTOOLS%\..\..\VC\vcvarsall.bat" x86_amd64
		
	msbuild /m:%processors% LLVM.sln /p:Configuration=!config! /p:Platform=!plat!
) ELSE (
	IF /I %version% == msvc9 (
		IF /I !plat! == Win32 (
			cmake -G "Visual Studio 9 2008" -DLLVM_TARGETS_TO_BUILD=X86 -DBUILD_SHARED_LIBS=FALSE -DLLVM_INCLUDE_EXAMPLES=FALSE -DLLVM_INCLUDE_TESTS=FALSE -DLLVM_INCLUDE_TOOLS=FALSE ..\..
		) ELSE (
			cmake -G "Visual Studio 9 2008 Win64" -DLLVM_TARGETS_TO_BUILD=X86 -DBUILD_SHARED_LIBS=FALSE -DLLVM_INCLUDE_EXAMPLES=FALSE -DLLVM_INCLUDE_TESTS=FALSE -DLLVM_INCLUDE_TOOLS=FALSE ..\..
		)
		
		SET "formattedBuildType=!config!|!plat!"

		CALL "%VS90COMNTOOLS%\..\..\VC\vcvarsall.bat" x86_amd64
		
		vcbuild /M%processors% LLVM.sln "!formattedBuildType!"
	)
)

xcopy /D /Y /S /EXCLUDE:%__local%\IgnoreTypes.txt ..\..\include\llvm %__local%\..\3rdParty\!version!\!plat!\llvm\
xcopy /D /Y /S /EXCLUDE:%__local%\IgnoreTypes.txt include\llvm %__local%\..\3rdParty\!version!\!plat!\llvm\
xcopy /D /Y /EXCLUDE:%__local%\IgnoreLibs.txt lib\!config!\LLVM*.lib %__local%\..\3rdParty\!version!\!plat!\!config!\llvm\

IF /I !config! == Debug (
	xcopy /D /Y /EXCLUDE:%__local%\IgnoreLibs.txt lib\!config!\LLVM*.pdb %__local%\..\3rdParty\!version!\!plat!\!config!\llvm\
)

GOTO End

:FailInvalidTarget

ECHO The target you asked to build (%2) was not valid (Win32|x64:Debug|Release)

GOTO Fail

:FailInvalidMSVCVersion

ECHO The version of msvc you asked to build (%3) was not valid (msvc9 or msvc10)

:Fail

ECHO "Usage: CreateLLVM path\to\llvm platform|configuration version"

GOTO Done

:End

CD /D %__original%

ECHO Started : %__startTime%
ECHO Ended   : %DATE% %TIME%

:Done

EndLocal